/*

            Author : Roy Raul.

            Component : LocaleProxy.             

            Version History         : 0.1 Date Jul-20-2011

            Current Version         : 0.1 Date Jul-20-2011

            Build Date              : Jul-20-2011

*/
package com.micmac.main.mainmodel.service{
	
	import com.micmac.main.interfaces.IStartupLocaleService;
	import com.micmac.main.interfaces.IStartupService;
	import com.micmac.main.mainmodel.business.LoadXMLDelegate;
	import com.micmac.main.mainmodel.helpers.XmlResource;
	import com.micmac.main.mainmodel.interfaces.IStartupModel;
	
	import mx.rpc.IResponder;
	
	import org.robotlegs.mvcs.Actor;
	
	

    /**
     * A proxy for read the resource file
     */
     
    public class LocaleProxy extends  Actor implements IStartupLocaleService, IResponder{
		public static const NAME:String = "LocaleProxy";
		
		[Inject]
		public var startupMonitorModel:IStartupModel;
		[Inject]
		public var configService:IStartupService;
		private var data:Object;
		
		public var errorStatus:String;
		public function register():void
		{
			startupMonitorModel.addResource( this , true );
		}
		
        
		public function load():void{
			
			var url:String =  "assets/data/"+ configService.getValue('language')+'.xml';
			this.data = new Object();
			// create a worker who will go get some data
			// pass it a reference to this proxy so the delegate knows where to return the data
			var delegate : LoadXMLDelegate = new LoadXMLDelegate(this, url);
			// make the delegate do some work
			delegate.load();
		}
		
		// this is called when the delegate receives a result from the service
		public function result( rpcEvent : Object ) : void{
			// call the helper class for parse the XML data
			XmlResource.parse(data, rpcEvent.result);
			// call the StartupMonitorProxy for notify that the resource is loaded
			startupMonitorModel.resourceComplete( this );
		}
		
		// this is called when the delegate receives a fault from the service
		public function fault( rpcEvent : Object ) : void{
			// store the error message
			// send the failed notification
		}

		/**
         * Get the localized text
		 * 
         * @param key the key to read 
         * @return String the text value stored in internal object
         */
         
		public function getText(key:String):String{
			return data[key.toLowerCase()];
		}
		

	}
}